import time

from playwright.sync_api import Playwright, sync_playwright, expect
from bs4 import BeautifulSoup as bs
import requests

live_ids = []
def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://live.douyin.com/")
    page.get_by_role("heading", name="推荐直播").click()
    for _ in range(10):  # 根据需要调整次数
        page.mouse.wheel(0, 1000)
        page.wait_for_timeout(1000)  # 每次按键后等待加载
    move_viewport(page)
    content = page.content()
    soup = bs(content, 'html.parser')
    for a in soup.find_all('a', href=True):
        href = a['href']
        if "https://live.douyin.com/category" in href:
            category_spider(page, href)
        elif "https://live.douyin.com/" in href and href.split('/')[-1].isdigit():
            live_ids.append(href.split('/')[-1])
    context.close()
    browser.close()


def category_spider(page, url):
    page.goto(url)
    # for _ in range(3):  # 根据需要调整次数
    #     page.mouse.wheel(0, 1000)
    #     page.wait_for_timeout(1000)  # 每次按键后等待加载
    content = page.content()
    soup = bs(content, 'html.parser')
    for a in soup.find_all('a', href=True):
        href = a['href']
        if "https://live.douyin.com/" in href and href.split('/')[-1].isdigit():
            live_ids.append(href.split('/')[-1])


def move_viewport(page):
    viewport_size = page.viewport_size
    if viewport_size:
        width = viewport_size['width']
        height = viewport_size['height']

        # 计算页面中心位置
        center_x = width / 2
        center_y = height / 2

        # 移动鼠标到页面中心
        page.mouse.move(center_x, center_y)



with sync_playwright() as playwright:
    run(playwright)
    live_ids = list(set(live_ids))
    for live_id in live_ids:
        requests.get(f"http://127.0.0.1:8000/api/dy/spider_script_run?live_id={live_id}")
